package com.exchange.used.config.utils;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.NumberToTextConverter;

public class StyleJud {
    public static String putCellValueToString(CellValue value) {
        if (value != null) {
            switch (value.getCellTypeEnum()) {
                case STRING:
                    return value.formatAsString();
                case NUMERIC:
                    return String.valueOf(value.getNumberValue());
                case BOOLEAN:
                    return String.valueOf(value.getBooleanValue());
            }
        }
        return "";
    }

    public static String putCellToString(Cell cell) {
        if (cell != null) {
            switch (cell.getCellTypeEnum()) {
                case BOOLEAN:
                    return String.valueOf(cell.getBooleanCellValue());
                case ERROR:
                case NUMERIC:
                    return NumberToTextConverter.toText(cell.getNumericCellValue());
                case STRING:
                    return cell.getRichStringCellValue().getString();
                case FORMULA:
                    Workbook workbook = cell.getSheet().getWorkbook();
                    FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
                    return putCellValueToString(evaluator.evaluate(cell));
            }
        }
        return "";
    }
}
